home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7731 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.4 KB  |  150 lines

  1. Path: pcnet.com!usenet
  2. From: Ron Lawrence <lawrenc@pcnet.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Class variables in C++
  5. Date: Fri, 16 Feb 1996 18:08:53 -0500
  6. Organization: PCNet -- Public Access Internet in Connecticut!
  7. Message-ID: <31250E85.71FAA57B@pcnet.com>
  8. References: <1996Feb14.165316@eigb> <3123D4BF.7C9@aai.arco.com>
  9. NNTP-Posting-Host: ts3-pt2.pcnet.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; Linux 1.2.1 i586)
  14.  
  15. Here are some options for creating "constants" in a class (in the order
  16. that they occurred to me):
  17.  
  18. 1) For integer constants (only), include a (possibly anonymous)
  19. enumeration:
  20. class X
  21. {
  22. public:
  23.   enum { INT_CONSTANT=123 };
  24. //or
  25.   enum namedEnum { P, Q };
  26. };
  27. N.B. this is the only way to create constants for declaring sizes
  28. for embedded arrays, like:
  29. class A
  30. {
  31. public:
  32. // ...
  33. private:
  34.   enum { BUFSIZE=1024 };
  35.   char buffer[BUFSIZE];
  36. };
  37.  
  38. 2) include a static const variable in the class declaration.  Define
  39. the constant in a source file somewhere.
  40. class Y
  41. {
  42. public:
  43. // ...
  44. private:
  45.   static const double DBL_CONSTANT;
  46. };
  47.  
  48. // in the .cc file:
  49. const double Y::DBL_CONSTANT=3.14159;
  50.  
  51. 3) include a constant member data element in the class declaration.
  52. This constant data member must be initialized as one of the initializers
  53. in each of the constructors defined for the class.  If 
  54. class Z
  55. {
  56. public:
  57.   Z();
  58.   Z(double);
  59. private:
  60.   const double DBL_CONSTANT;
  61. };
  62.  
  63. // in the .cc file:
  64. Z::Z() : DBL_CONSTANT(3.14159)
  65. {}
  66. Z::Z(double z): DBL_CONSTANT(z)
  67. {}
  68.  
  69. N.B. if you have a constructor that can be called which doesn't 
  70. initialize the constant it will remain uninitialized forever in the
  71. constructed object.  G++ 2.6.3 doesn't complain about this, I don't know
  72. if other compilers do.
  73.  
  74. 4) Include member functions that return the constant.  This technique
  75. is described below in Brian Leach's followup posting.
  76.  
  77. 5) You can also use the preprocessor to create constants, but this
  78. is deprecated in C++. e.g.
  79. #define DBL_CONSTANT 3.14159
  80.  
  81. These are the techniques that I am aware of.  Are there any others?
  82. If anyone knows of any, I'd like to know more about them.
  83.  
  84. I hope this helps out.
  85.  
  86.  
  87. Original postings follow:
  88. Brian Leach wrote:
  89. > guinnard@eigb wrote:
  90. > >
  91. > > Hi,
  92. > >
  93. > > Is it possible to create a class variable (or constant) in C++ ?
  94. > >
  95. > > For example:
  96. > >
  97. > > class ANIMAL
  98. > > {
  99. > >   protected:
  100. > >     <I would like a class-variable (or constant), please>
  101. > >     UINT uNumberOfLeg=0; /* pure ?!?! */
  102. > >
  103. > >   public:
  104. > >     ...
  105. > > };
  106. > >
  107. > > class CAT : public ANIMAL
  108. > > {
  109. > >   protected:
  110. > >     <I would like a class-variable (or constant), please>
  111. > >     UINT uNumberOfLeg=4;
  112. > >
  113. > >   public:
  114. > >     ...
  115. > > };
  116. > >
  117. > > Because I have never see a cat with 5 legs...
  118. > > (I don't want to reserve 2 or more bytes in each instance of this class to
  119. > > save the number of leg for a cat...)
  120. > >
  121. > > I hope this is comprehensive...
  122. > >
  123. > > Thanks
  124. > >
  125. > >                                                Laurent Guinnard
  126. > >                                                guinnard@eig.unige.ch
  127. > >
  128. > > be_well++;
  129. > class Animal {
  130. > public:
  131. >    size_t numberOfLegs() const =0;
  132. > };
  133. > class Cat: public Animal {
  134. > public:
  135. >    size_t numberOfLegs() const { return 4; }
  136. > };
  137. > class Spider: public Animal {
  138. > public:
  139. >    size_t numberOfLegs() const { return 8; }
  140. > };
  141. > There are other ways to do it, but this is the most straight-forward
  142. > I could come up with.
  143. > -Brian :-)
  144.